package Thread;

import java.util.Scanner;

public class Demo21 {
    public static void main3(String[] args) {
        Object locker = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(10000);
                System.out.println("wait 之前");
                synchronized (locker) {
                    locker.wait();
                }
                System.out.println("wait 之后");
            } catch (InterruptedException e) {
                throw new RuntimeException();
            }
        });

        Thread t2 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入任意内容, 通知唤醒 t1");
            scanner.next();

            synchronized (locker) {
                locker.notify();
            }
        });

        t1.start();
        t2.start();
    }

    public static void main(String[] args) {
        Object locker = new Object();
        Object locker2 = new Object();
        Thread t1 = new Thread(()->{
           try {
               Thread.sleep(10000);
               System.out.println("t1线程等待中，在wait之前");
               synchronized (locker){
                   locker.wait();
               }
               System.out.println("t1线程等待结束，在wait之后");
           }catch (InterruptedException e){
               throw new RuntimeException();
           }
        });
        Thread t2 = new Thread(()->{
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入任意内容，通知唤醒t1");
            scanner.next();
            synchronized (locker){
                System.out.println("t2在准备唤醒t1");
//               try {
//                   locker2.wait();
//               } catch (InterruptedException e) {
//                   throw new RuntimeException(e);
//               }
                locker.notify();
                // System.out.println("t2没有唤醒t1，而是唤醒自己的锁");
            }
        });
        t1.start();
        t2.start();
    }
    public static void main1(String[] args)throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Thread t1 = new Thread(()->{
            System.out.println("t1线程等待中，在wait之前");
            synchronized (locker1){
                try {
                    locker1.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1线程等待结束，在wait之后");
            }
        });
        Thread t2 = new Thread(()->{
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入任意内容，通知唤醒t1");
            scanner.next();
           synchronized (locker1){
               System.out.println("t2在准备唤醒t1");
//               try {
//                   locker2.wait();
//               } catch (InterruptedException e) {
//                   throw new RuntimeException(e);
//               }
               locker1.notify();
              // System.out.println("t2没有唤醒t1，而是唤醒自己的锁");
           }
        });
        t1.start();
        t2.start();
    }
}
